package com.xiaoyu.izf.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.xiaoyu.izf.model.page.FormResult;
import com.xiaoyu.izf.service.UploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * 文件上传接口
 */
@RestController
@RequestMapping("/api/upload")
public class UploadController {

    @Autowired
    private UploadService uploadService;

    /**
     * 上传图片
     * @param request
     * @param multipartFile
     * @return
     */
    @PostMapping("/image")
    public FormResult uploadImage(HttpServletRequest request, @RequestParam("image") MultipartFile multipartFile){
        JSONObject jsonObject = uploadService.uploadImg(request,multipartFile);
        return new FormResult(jsonObject.getIntValue("status"),
                jsonObject.getString("msg"),jsonObject.getString("img"));
    }
}
